package {

public class TextFile {

    public var text:String;
    private var punctuationArray:Array = [' ', '.', ',', '?', '!', '-', ';', ':', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

    function createText():String {

        text = 'Copy the text and paste it here. After that push the "Start" button. ' +
                'You can learn the text using this program. ' +
                'It helps to remember how to write the words. ' +
                'Good luck :-) ';

        return text;

    }

    function startGame():String {

        var lenght:int;
        var result:String = new String("");
        var copyText:String = text;

        for (lenght = 0; lenght < copyText.length - 1; lenght++) {
            if (isGoodChar(copyText.charAt(lenght))) {
                result += "*";
            } else {
                result += copyText.charAt(lenght);
            }
        }
        return result;
    }

    function searchWord(allText:String, word:String):String {

        var result:String = new String("");
        var textLowerCase:String = text.toLowerCase();

        if (textLowerCase.indexOf(word) == -1) {
            return allText;
        } else {

            var i:int = 0;

            do
            {
                i = textLowerCase.indexOf(word, i);
                if (i != -1) {
                    if (isGoodWord(word, i)) {
                        var j:int = i;

                        result += allText.substring(result.length, i);
                        for (j; j < i + word.length + 1; j++) {
                            result += text.charAt(j);
                        }
                    }
                    i++;
                } else {
                    result += allText.substring(result.length, allText.length);
                }
            }
            while (i != -1);

            return result;
        }
    }

    function isGoodWord(wordVar:String, pos:int):Boolean {

        var isGood:Boolean = false;

        for (var i:Number = 0; i < punctuationArray.length; i++) {
            switch (pos) {
                case 0:
                    try {
                        if (text.charAt(wordVar.length) == punctuationArray[i]) {
                            isGood = true;
                        }
                    }
                    catch (error:Error) {
                        // handle error by rethrowing
                        throw error;
                    }
                case text.length - wordVar.length:
                    try {
                        if (text.charAt(pos - 1) == punctuationArray[i]) {
                            isGood = true;
                        }
                    }
                    catch (error:Error) {
                        // handle error by rethrowing
                        throw error;
                    }
                default:
                    if (text.charAt(wordVar.length + pos) == punctuationArray[i] && !isGoodChar(text.charAt(pos - 1))) {
                        isGood = true;
                    }
            }
        }
        return isGood;
    }

    function isGoodChar(charOne:String):Boolean {

        var isGood:Boolean = true;

        for (var i:Number = 0; i < punctuationArray.length; i++) {
            if (charOne == punctuationArray[i]) {
                isGood = false;
            }
        }
        return isGood;
    }
}
}

